package org.ogv;

import java.util.Random;

public class Spawner<T> {

	private T[] 	_pool;
	private Random 	_r;
	private float 	_time, _dev, _timeLeft;
	
	public Spawner() {
		_r = new Random();
	}
	
	public Spawner<T> init(T[] pool, float time, float dev) {
		_pool 	= pool;
		_time 	= time;
		_dev	= dev;
		return this;
	}
	
	public void reset() {
		_timeLeft = _time + _r.nextFloat() * _dev - _dev * 0.5f;
	}
	
	public boolean update(float deltaTime) {
		_timeLeft -= deltaTime;
		if(_timeLeft <= 0) {
			reset();
			return true;
		}
		
		return false;
	}
	
	public T getSpawn() {
		return _pool[_r.nextInt(_pool.length)];
	}
}
